package _HC_experiments;

import java.util.Vector;

public class HCUtils {

	private static int preDot(String _s, int _cid) {
		int rid = _cid;
		if (_cid >= _s.length()) {
			rid = _s.length() - 1;
		}
		int bc = 0;
		for (; rid >= 0; --rid) {
			if (_s.charAt(rid) == ']') {
				--bc;
				continue;
			}
			if (_s.charAt(rid) == '[') {
				++bc;
				continue;
			}
			if (bc == 0 && _s.charAt(rid) == '.') {
				break;
			}
		}
		return rid;
	}

	/**
	 * 
	 * 
	 * @param _fileName
	 * @return [data][rs][split_method][tr|ts][sampling_method][arff]
	 */
	public static String[] arffFileNameSplit(String _fileName) {
		String[] ret = new String[6];
		int rid = 5;
		int cid, pid;

		cid = _fileName.length() - 1;
		pid = preDot(_fileName, cid);
		ret[rid--] = _fileName.substring(pid + 1, cid + 1);// "arff"

		cid = pid - 1;
		pid = preDot(_fileName, cid);
		String tmpS = _fileName.substring(pid + 1, cid + 1);
		if (tmpS.equals("tr") || tmpS.equals("ts")) {// [sampling_method]
			ret[rid--] = "NONE";
		} else {
			ret[rid--] = tmpS;
			cid = pid - 1;
			pid = preDot(_fileName, cid);
		}
		ret[rid--] = _fileName.substring(pid + 1, cid + 1);// [tr]

		cid = pid - 1;
		pid = preDot(_fileName, cid);
		ret[rid--] = _fileName.substring(pid + 1, cid + 1);// [split_method]

		cid = pid - 1;
		pid = preDot(_fileName, cid);
		ret[rid--] = _fileName.substring(pid + 1, cid + 1);// [rs]

		cid = pid - 1;
		pid = preDot(_fileName, cid);
		ret[rid--] = _fileName.substring(0, cid + 1);// [data]

		return ret;
	}

	
	/**
	 * 
	 * 
	 * 
	 * @param _fileName
	 * @return [data][rs][split_method][tr|ts][sampling_method][classifier][log]
	 */
	public static String[] logFileNameSplit(String _fileName){
		
		String[] ret = new String[7];
		int rid = 6;
		int cid, pid;

		cid = _fileName.length() - 1;
		pid = preDot(_fileName, cid);
		ret[rid--] = _fileName.substring(pid + 1, cid + 1);// "log"
		
		cid = pid - 1;
		pid = preDot(_fileName, cid);
		ret[rid--] = _fileName.substring(pid + 1, cid + 1);// [classifier]

		cid = pid - 1;
		pid = preDot(_fileName, cid);
		String tmpS = _fileName.substring(pid + 1, cid + 1);
		if (tmpS.equals("tr") || tmpS.equals("ts")) {// [sampling_method]
			ret[rid--] = "NONE";
		} else {
			ret[rid--] = tmpS;
			cid = pid - 1;
			pid = preDot(_fileName, cid);
		}
		ret[rid--] = _fileName.substring(pid + 1, cid + 1);// [tr]

		cid = pid - 1;
		pid = preDot(_fileName, cid);
		ret[rid--] = _fileName.substring(pid + 1, cid + 1);// [split_method]

		cid = pid - 1;
		pid = preDot(_fileName, cid);
		ret[rid--] = _fileName.substring(pid + 1, cid + 1);// [rs]

		cid = pid - 1;
		pid = preDot(_fileName, cid);
		ret[rid--] = _fileName.substring(0, cid + 1);// [data]

		return ret;
	}

	public static void pushBack(Vector _vec, Object[] _arr){
		for (int i = 0; i < _arr.length; ++i){
			_vec.add(_arr[i]);
		}
	}
	
	public static void pushBack(Vector _vec, Vector _arr){
		for (int i = 0; i < _arr.size(); ++i){
			_vec.add(_arr.get(i));
		}
	}
	
	public static void main(String args[]) {
//		String[] tmp = arffFileNameSplit("imba-yeast.dat.rs.cv5f0.tr.smote[5nn][50.0].arff");
//		for (int i = 0; i < tmp.length; ++i) {
//			System.out.println("1----" + tmp[i]);
//		}
//		tmp = arffFileNameSplit("imba-yeast.dat.rs.cv5f0.tr.arff");
//		for (int i = 0; i < tmp.length; ++i) {
//			System.out.println("2----" + tmp[i]);
//		}
		
		String[] tmp = logFileNameSplit("imba-abalone.data.rs.cv5f0.tr.oss[1nn].smo[linear][bp].log");
		for (int i = 0; i < tmp.length; ++i) {
			System.out.println("1----" + tmp[i]);
		}
		tmp = logFileNameSplit("imba-abalone.data.rs.cv5f0.tr.smo[linear][bp].log");
		for (int i = 0; i < tmp.length; ++i) {
			System.out.println("2----" + tmp[i]);
		}
	}

}
